package com.groupseven.backend.controller;


import com.groupseven.backend.domain.ResultType;
import com.groupseven.backend.domain.SysDictData;
import com.groupseven.backend.services.ISysDictDataService;
import com.groupseven.backend.services.ISysDictTypeService;
import com.groupseven.backend.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 *
 */
@RestController
@RequestMapping("/api/v1/dict/data")
public class SysDictDataController extends BaseController
{
    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;




    @GetMapping("/list")
    public ResultType list(SysDictData dictData)
    {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        ResultType resultType=new ResultType(1,"查询成功",200);
        resultType.setSysDictDatas(list);
        return resultType;
    }

    /**
     * 查询字典数据详细
     */
    @GetMapping(value = "/{dictCode}")
    public ResultType getInfo(@PathVariable Long dictCode)
    {

        SysDictData sysDictData=dictDataService.selectDictDataById(dictCode);
        ResultType resultType=new ResultType(1,"查询成功",200);
        resultType.setSysDictData(sysDictData);
        return resultType;
    }



    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public ResultType dictType(@PathVariable String dictType)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<SysDictData>();
        }
        ResultType resultType=new ResultType(1,"查询成功",200);
        resultType.setSysDictDatas(data);
        return resultType;
    }

    /**
     * 新增字典类型
     */
    @PostMapping
    public ResultType add(@Validated @RequestBody SysDictData dict)
    {
        dictDataService.insertDictData(dict);
        return new ResultType(1,"插入成功",200);
    }

    /**
     * 修改保存字典类型
     */
    @PutMapping
    public ResultType edit(@Validated @RequestBody SysDictData dict)
    {
        dictDataService.updateDictData(dict);
        return new ResultType(1,"修改成功",200);
    }

    /**
     * 删除字典类型
     */
    @DeleteMapping("/{dictCodes}")
    public ResultType remove(@PathVariable Long[] dictCodes)
    {
        dictDataService.deleteDictDataByIds(dictCodes);
        return new ResultType(1,"删除成功",200);
    }

    /**
     * 根据字典类型和值查询字典数据信息
     */
    @GetMapping(value = "/search/{dictValue}")
    public ResultType selectDictLabel(@PathVariable String dictValue){
        List<SysDictData> data = dictDataService.selectDictLabel(dictValue);
        ResultType resultType=new ResultType(1,"查询成功",200);
        resultType.setSysDictDatas(data);
        return resultType;
    }
}
